var app_open = 0;
var email_open = 0;
var tel_open = 0;
var weChat_open = 0;
var url = "http://112.124.61.147:808/2";
var authkey = request("authkey");
var b=null;
var Relation_mail;
var lg=request("language");
function request(paras) {
    var url = location.href;
    var paraString = url.substring(url.indexOf("?")+1,url.length).split("&");
    var paraObj = {}
    for (i=0;j=paraString[i];i++){
        paraObj[j.substring(0,j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=")+1,j.length);
    }
    var returnValue = paraObj[paras.toLowerCase()];
    if(typeof(returnValue)=="undefined"){
        return "";
    }else{
        return returnValue;
    }
}
function gettel(){             //获取电话信息
    $.ajax({
        type: "POST",
        url: "http://112.124.61.147:808/2/showAlarm/address",
        data: JSON.stringify({"authkey":authkey,"param":"phone"}),
        dataType: "json",
        success: function (response) {
            var phone = response.result;
            if((phone['bindtel']).length>0 && (phone['bindtel'])!="null"){
                if((phone['Relation_tel']).length!=0 && (phone['Relation_tel'])!= "null"){
                    window.location.href="phone2.html?&authkey="+authkey;
                }else{
                    window.location.href="phone3.html?&authkey="+ authkey;
                }
            }else {
                window.location.href="phone1.html?tel1="+phone['bindtel']+"&tel2="+phone['Relation_tel']+"&authkey="+ authkey;
            }
        },error:function(response){
            swal({
                //title: "获取电话信息失败！",
            })
        }
    });
}
function queren(){               //绑定电话
    var tel=$("#phone").val();
    var yzm=$("#yzm").val();
    $.ajax({
        type: "POST",
        url: "http://112.124.61.147:808/2/users/Bandtel",
        data: JSON.stringify({"authkey":authkey,"phone":tel,"verifi":yzm,"pre":"1"}),
        dataType: "json",
        success: function (response) {
            var cuo = response['errcode'];
            if (cuo == 0) {
                swal({
                    title: "bangding ！",
                })
            }
        },error:function(response){
            swal({
                title: "绑定失败！",
            })
        }
    });
}

function queren2(){               //绑定电话
    var tel=$("#phone").val();
    var yzm=$("#yzm").val();
    $.ajax({
        type: "POST",
        url: "http://112.124.61.147:808/2/users/Bandtel",
        data: JSON.stringify({"authkey":authkey,"phone":tel,"verifi":yzm,"pre":"2"}),
        dataType: "json",
        success: function (response) {
            var cuo = response['errcode'];
            if (cuo == 0) {
                swal({
                    title: "bangding ！",
                })
            }else if (cuo == 4143) {
                alert("验证码错误 ！");
            }else{
                alert("验证失败！")
            }
        },error:function(response){
            swal({
                title: "绑定失败！",
            })
        }
    });
}

function deleteph(){      //删除电话绑定
    $.ajax({
        type: "POST",
        url: url + "/deleteAlarm/address",
        data: JSON.stringify({"authkey":authkey,"param":"Relation_tel"}),
        dataType: "json",
        success: function (response) {
            var cuo = response['errcode'];
            if(cuo==0){
                alert(1243);
                gettel();
            }
        },error:function(response){
            swal({
                title: "删除绑定失败！",
            })
        }
    });
}

function getEmail(){          //获取邮件信息
    $.ajax({
        type: "POST",
        url: "http://112.124.61.147:808/2/showAlarm/address",
        data: JSON.stringify({"authkey":authkey,"param":"email","verifi":"verifi_mail"}),
        dataType: "json",
        success: function (response) {
            var email = response.result;
            window.location.href="email.html?param1="+email['Relation_mail1']+"&param2="+email['Relation_mail2']+"&verifi="+email['verifi_mail']+"&param3="+email['Relation_mail3']+"&param4="+email['Relation_mail4']+"&param5="+email['Relation_mail5']+"&email="+email['email']+"&authkey="+ authkey+"&language="+ lg;
        },error:function(response){
            alert("获取邮件信息失败！")
        }
    });
}

function  addemail(){                 //发送邮件验证    //判断是否待验证
    if(request("verifi").length>0 && request("verifi")!="null"){
        swal({
            title: "请您先验证邮箱！",
        })
        return;
    }
    var tj=$(".tjc").val();
    $.ajax({
        type: "POST",
        url: "http://112.124.61.147:808/2/code/email2",
        data: JSON.stringify({"authkey":authkey,"email":tj,"language":lg}),
        dataType: "json",
        success: function (response) {
            var cuo = response['errcode'];
            if(cuo==0){
                swal({
                    title: "验证码发送成功！快去邮箱验证吧！",
                },function(){
                    $(".ul-email").append('<li class=' + Relation_mail + ' name='+ tj +'>'+'<a>'+tj +'</a>'+'<em>'+'</em>'+'<span>'+'</span>'+ _email_bind_dyz +'</li>');
                })
            }else if(cuo==4043){
                swal({
                    title: "授权码已过期！请重新登录！",
                })
            }else if(cuo==4085){
                swal({
                    title: "验证码发送超过5次！",
                })
            }else if(cuo==4010){
                swal({
                    title: "格式不正确！",
                })
            } else{
                swal({
                    title: "验证码发送失败！",
                })
            }
        },error:function(response){
            swal({
                title: "验证码发送失败！",
            })
        }
    });
}
function  addemails(){                //重新发送邮件验证
    dyz=request("verifi");
    alert(dyz);
    $.ajax({
        type: "POST",
        url: "http://112.124.61.147:808/2/code/email2",
        data: JSON.stringify({"authkey":authkey,"email":dyz,"language":lg}),
        dataType: "json",
        success: function (response) {
            var cuo = response['errcode'];
            if(cuo==0){
                swal({
                    title: "验证码发送成功！快去邮箱验证吧！",
                },function(){
                })
            }else if(cuo==4043){
                swal({
                    title: "授权码已过期！请重新登录！",
                })
            }else if(cuo==4085){
                swal({
                    title: "验证码发送超过5次！",
                })
            }else if(cuo==4087){
                swal({
                    title: "地址已绑定！请删除重绑！",
                })
            }else{
                swal({
                    title: "验证码发送失败！",
                })
            }
        },error:function(response){

        }
    });
}
function deleteem() {      //删除电子邮件
    var prams=null;
    if(request("verifi")!=null && request("verifi").length>0){
        var re="."+b;
        if($(re).attr("name")==(request("verifi"))){
            prams="verifi_mail";
        }
    }
    $.ajax({
        type: "POST",
        url: "http://112.124.61.147:808/2/deleteAlarm/address",
        data: JSON.stringify({"authkey": authkey, "param": b,"verifi":prams}),
        dataType: "json",
        success: function (response) {
            swal({
                title: "确定删除此邮箱吗？",
                showCancelButton: true
            },function (){
                $("." + b).remove();
                    $(".outbox").slideUp(200);                    //****
                    $(".cens").hide();                            //****
            })
        },error:function(response){
            swal({
                title: "删除失败！"
            })
        }
    });
}

function Jump_page (param){         //跳转页面
    if (param == "app_open"){
        if (app_open == 1){
            window.location.href="client.html"
        }else{
            $(".one").hide();
            $(".ones").show();
            $(".onees").hide();
        }
    }
    if (param == "email_open"){
        if (email_open == 1){
            getEmail();
        }else{
            $(".twos").hide();
            $(".two").hide();
            $(".twoes").show();
        }
    }
    if (param == "tel_open"){
        if (tel_open == 1){
            gettel();
        }else{
            $(".threes").hide();
            $(".three").hide();
            $(".threeis").show();
        }
    }
}

function get_state(){
    $.get(url + "/open_page/service/" +authkey ).success(function(response){    //获取后台数据
        if(response.ret = 1){
            var switch_type = response.switch;
            var cuo = response['errcode'];
            if(cuo==4043){
                swal({
                    title: "authkey失效",
                },function(){
                })
            }
            app_open  = switch_type['app_open'];
            email_open  = switch_type['email_open'];
            tel_open  = switch_type['tel_open'];
            weChat_open  = switch_type['weChat_open'];
            var tel_num  = switch_type['tel_num'];
            $("#num").append(tel_num);
            if(app_open==1){
                $(".ones").hide();
                $(".one").hide();
                $(".onees").show();
            }else{
                $(".ones").hide();
                $(".one").show();
                $(".onees").hide();
            }
            if(email_open==1){
                $(".twos").hide();
                $(".two").hide();
                $(".twoes").show();
            }else{
                $(".twos").hide();
                $(".two").show();
                $(".twoes").hide();
            }
            if(tel_open==1){
                $(".threes").hide();
                $(".three").hide();
                $(".threeis").show();
            }else{
                $(".threes").hide();
                $(".three").show();
                $(".threeis").hide();
            }
            if(weChat_open==1){
                $(".fours").hide();
                $(".four").hide();
                $(".foures").show();
            }else{
                $(".fours").hide();
                $(".four").show();
                $(".foures").hide();
            }
        }else {
            alert("获取后台数据失败！")
        }
    });
}

function tf1(){  //点击跳到背面
    $(".one").hide();
    $(".ones").show();
    $(".onees").hide();
}
function tf2(){  //点击跳到背面
    $(".two").hide();
    $(".twos").show();
    $(".twoes").hide();
}
function tf3(){  //点击跳到背面
    $(".three").hide();
    $(".threes").show();
    $(".threeis").hide();
}
function tf4(){  //点击跳到背面
    $(".four").hide();
    $(".fours").show();
    $(".foures").hide();
}
function tz1(){  //点击跳到正面
    $(".ones").hide();
    $(".one").show();
    $(".onees").hide();
}
function tz2(){  //点击跳到正面
    $(".twos").hide();
    $(".two").show();
    $(".twoes").hide();
}
function tz3(){  //点击跳到正面
    $(".threes").hide();
    $(".three").show();
    $(".threeis").hide();
}
function tz4(){  //点击跳到正面
    $(".fours").hide();
    $(".four").show();
    $(".foures").hide();
}

function server_open(param)    {   //  开通服务
    $.get( url + "/switch_alarm/service/" + authkey + "/" + param + "/1").success(function(){
        if (param == "app_open"){
            app_open = 1;
            swal({
                title: "已成功开通客户端服务通知."
            },function (){
                $(".ones").hide();
                $(".one").hide();
                $(".onees").show();
            })
        }else if (param == "email_open"){
            email_open = 1;
            swal({
                title: "已成功开通邮件通知."
            },function (){
                getEmail();
            })
        }else if (param == "tel_open"){
            tel_open = 1;
            swal({
                title: "已成功开通电话通知."
            },function (){
                gettel();
            })
        }else if (param == "weChat_open"){
            weChat_open = 1;
            swal({
                title: "已成功开通微信通知."
            },function (){
                $(".fours").hide();
                $(".four").hide();
                $(".foures").show();
            })
        }
    })
}

function server_close(param) {      //关闭服务
    var txt ="";
    if (param == "app_open"){
        txt = "确认关闭客户端服务通知吗?"
    }else if (param == "email_open") {
        txt = "确认关闭邮件通知吗?"
    }else if (param == "tel_open") {
        txt = "确认关闭电话通知吗?"
    }else if (param == "weChat_open") {
        txt = "确认关闭微信端通知吗?"
    }
        swal({
            title: txt,
            showCancelButton: true
        },function (){
            $.get( url + "/switch_alarm/service/" + authkey +"/" + param + "/0").success(function(){
                if (param == "app_open"){
                    app_open = 0;
                    $(".ones").hide();
                    $(".one").show();
                    $(".onees").hide();
                }else if (param == "email_open"){
                    email_open = 0;
                    $(".twos").hide();
                    $(".two").show();
                    $(".twoes").hide()
                }else if (param == "tel_open"){
                    tel_open = 0;
                    $(".threes").hide();
                    $(".three").show();
                    $(".threeis").hide();
                }else if (param == "weChat_open"){
                    weChat_open = 0;
                    $(".fours").hide();
                    $(".four").show();
                    $(".foures").hide();
                }
            });
        }
    )
}







